package com.mactivity.database;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.mactivity.helper.Constants;
/*
 * This AddFriendActivityFunction class contains calls to database used in Add Friend Module
 *  
 */
public class AddFriendActivityFunctions {
	
	/*
	 * Finds if the user is valid, if yes returns the details of the user
	 * 
	 * @param emailId
	 * EmailId enter by the user
	 * returns details of user on success
	 * and null on failure
	 * 
	 */
	String response=null;
	public String findUser(String emailId){
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDEMAILID,emailId));
		try
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/find_user_script.php",postParameters);
			response = response.replaceAll("\n", "");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
	
	public String findUserByName(String name){
		
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDNAME,name));
		try
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/find_user_by_name_script.php",postParameters);
			response = response.replaceAll("\n", "");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
	
	/*
	 *  Fetches the UserId of the user 
	 * @param emailId
	 * EmailId enter by the user
	 * returns id of user on success
	 * and null on failure
	 * 
	 * 
	 */
	public String fetchId(String emailId)
	{
		ArrayList<NameValuePair>postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDEMAILID,emailId));
		String response=null;
		try 
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/check_user_script.php",postParameters);
			response = response.replaceAll("\n", "");
		}	
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
	
	/*
	 * checks if they are friends already
	 * @param myid, friendid
	 * 
	 * returns 1 if success , 0 if not friends
	 * 
	 */
	public String checkIfFriends(String myId,String frId)
	{
		ArrayList<NameValuePair>postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_MYID,myId));
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDID,frId));
		String response=null;
		try
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/friend_check_script.php",postParameters);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
	/*
	 * Checks if friend request has already been sent
	 * @param myid ,friendId
	 * returns 1 if already sent, 0 if not
	 * 
	 */
	public String checkNotification(String myId,String frId)
	{
		ArrayList<NameValuePair>postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_MYID,myId));
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDID,frId));
		String response=null;
		try
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/check_notification_request_script.php",postParameters);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
	
	/*
	 * this function adds the friend request notification to the database
	 * @param myid,friendid
	 * returns 1 if successfully added, 0 on failure
	 * 
	 */
	public String addNotification(String myId,String frId)
	{
		ArrayList<NameValuePair>postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_MYID,myId));
		postParameters.add(new BasicNameValuePair(Constants.CONSTANT_EXTRAS_FRIENDID,frId));
		String response = null;
		try
		{
			response = CustomHttpClient.executeHttpPost(Constants.DATABASE_URL+"AddFriend/add_friend_notification_script.php",postParameters);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return response;
	}
}
